﻿
//全局的json状态码
var jsonCode = { success: 200, fail: 201, error: 500 };

function ajaxGet(url, callBack) {
    Ajax(url, "", "GET", callBack);
}

function ajaxPost(url, param, callBack) {
    Ajax(url, param, "POST", callBack);
}

function Ajax(url, paramet, method, callBack) {
    wait.show("加载中");//显示等待框
    $.ajax({
        url: url,
        type: method,
        dataType: "json",
        data: encodeURI(paramet),
        success: function (json) {
            wait.dismiss();
            callBack(json);     //json方式回调
        },
        error: function (rs) {
            toast.show("请求异常!");
        }
    });
}


//Jsonp格式请求
function ajaxJsonp(url, paramet, callBack) {
    var dataType = "jsonp";
    //显示等待框
    wait.show("加载中");
    $.ajax({
        url: url,
        type: 'get',
        dataType: dataType,
        data: encodeURI(paramet),
        jsonpCallback: "JsonpCallback", //自定义的jsonp回调函数名称，默认为jQuery自动生成的随机函数名，也可以写"?"，jQuery会自动为你处理数据
        success: function (json) {
            wait.dismiss();
            callBack(json);     //json方式回调
        },
        error: function (rs) {
            toast.show("请求异常!");
        }
    });
}

//jsonp请求时,回调的空方法
function JsonpCallback(data, callback) {
}


//等待对话框
var wait = new Wait();
function Wait() {
    //等待框html代码
    var _wait_html = '<div id="floatWait"><div style="width: 100%; height: 100%; background-color: Black; position: fixed;z-index: 9999; opacity: 0.3;display:none;"></div><div style="width: 100%; height: 45px; left: 0; top: 50%; position: fixed; z-index: 1000;"align="center"><div style = "padding:10px; border-radius:5px; line-height:30px; background-color:Black; color:White; width:200px; display:block; opacity:0.8;"><span id = "msg"></span><span id="dot"></span></div></div></div>';
    this.isShow = false;
    this.show = function (msg) {
        if ($("#dot").length < 1) {
            $("body").prepend(_wait_html);
        }
        else {
            $("#floatWait").css("display", "");
        }
        $("#msg").text(msg);
        if (!this.isShow) {
            this.isShow = true;
            this.action();
        }
    }

    //动画
    this.action = function () {
        if ($("#dot").text().length > 5)
            $("#dot").text("·");
        else
            $("#dot").text($("#dot").text() + "·");

        if (this.isShow) {
            setTimeout(function () { wait.action(); }, 500);
        }
    }

    this.dismiss = function () {
        this.isShow = false;
        $("#floatWait").css("display", "none");
    }
}